<?php

namespace IDock\Insurance\QHIC\Response;

use IDock\Http\Response;
use IDock\Printer\NonCar\NonCarProductListPrinter;
use IDock\Printer\NonCar\NonCarProductPrinter;
use IDock\Printer\NonCar\NonCarSubCoveragePrinter;

class NonCarProductListResponse extends AllQHICResponse
{

    public $printer = NonCarProductListPrinter::class;

    public function printerInit(): Response
    {
        $body = $this->getBody();

        $list = $body['BaseList'] ?? [];
        if (!$list) {
            return $this;
        }

        foreach ($list as $item) {
            $productPrinter = new NonCarProductPrinter();
            $productPrinter->productCode = $item['CProdNo'] ?? '';//产品编码
            $productPrinter->productName = '';//产品名称
            $productPrinter->productType = '驾意险';
            $productPrinter->productDesc = '';
            $productPrinter->planCode = $item['CRuleNo'] ?? '';//方案代码
            $productPrinter->planName = $item['CRuleNme'] ?? '';//方案名称
            $productPrinter->maxQuantity = 1;//最大购买份数
            foreach ($item['CvrgList'] ?? [] as $sub) {
                $subCoverage = new NonCarSubCoveragePrinter();
                $subCoverage->subCode = $sub['CCvrgNo'] ?? '';
                $subCoverage->subName = $sub['CCvrgName'] ?? '';
                $subCoverage->amount = $sub['NAmt'] ?? '';
                $subCoverage->premium = $sub['NPrm'] ?? '';
                $productPrinter->subCoverageAppend($subCoverage);
            }
            $productPrinter->premium = array_sum(array_column($item['CvrgList'] ?? [], 'NPrm'));
            $productPrinter->unitAmount = array_sum(array_column($item['CvrgList'] ?? [], 'NAmt'));
            $this->printer->productListAppend($productPrinter);
        }

        return $this;
    }
}
